package cn.lingyangwl.agile.infra.gen.controller;

import cn.hutool.core.io.FileUtil;
import cn.lingyangwl.agile.infra.gen.manager.*;
import cn.lingyangwl.agile.infra.gen.model.rqrs.*;
import cn.lingyangwl.agile.infra.gen.service.*;
import cn.lingyangwl.agile.ram.*;
import cn.lingyangwl.framework.core.response.*;
import cn.lingyangwl.framework.tool.core.exception.*;
import cn.lingyangwl.framework.validation.group.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Collections;
import java.util.List;

/**
 * 代码生成器
 * @author shenguangyang
 */
@Api(tags = "生成代码的表")
@Validated
@RestController
@RequestMapping("/genTable")
public class GenTableController {
    @Resource
    private IGenTableService genTableService;
    @Resource
    private GenTableIndexService genTableIndexService;

    @Resource
    private GenTableColumnService genTableColumnService;

    @ApiOperation(value = "查询生成代码的表列表")
    @RamResource(action = "lcp:genTable:query", name = "低代码-查询生成代码的表", type = ActionType.READ)
    @GetMapping("/pageGenTable")
    public Resp<IPage<GenTableDto>> pageGenTable(@Validated GenTablePageReq req) {
        IPage<GenTableDto> resp = genTableService.pageGenTable(req);
        return Resp.ok(resp);
    }

    @ApiOperation(value = "获取查询生成代码的表详情")
    @RamResource(action = "lcp:genTable:query", name = "低代码-查询生成代码的表", type = ActionType.READ)
    @GetMapping(value = "/getGenTable")
    public Resp<GenTableDto> getGenTableByTableId(@RequestParam("tableId") Long tableId) {
        GenTableDto resp = genTableService.getGenTableRespByTableId(tableId);
        return Resp.ok(resp);
    }

    @ApiOperation(value = "低代码-更新生成代码信息")
    @RamResource(action = "lcp:genTable:update", name = "低代码-更新生成代码信息", type = ActionType.WRITE)
    @PostMapping("/update")
    public Resp<?> updateGenTable(@RequestBody @Validated(UpdateGroup.class) GeneratorReq req) {
        genTableService.updateGenTable(req);
        return Resp.ok();
    }

    @ApiOperation(value = "预览代码")
    @RamResource(action = "lcp:genTable:previewCode", name = "低代码-预览代码", type = ActionType.READ)
    @GetMapping("/previewCode")
    public Resp<?> previewCode() {
        System.out.println(genTableService.previewCode(Collections.singletonList(22L))
                .values().stream().findFirst().orElse(""));
        return Resp.ok();
    }

    @ApiOperation(value = "导入文档中的表")
    @RamResource(action = "lcp:genTable:importDocumentTable", name = "低代码-导入文档中的表", type = ActionType.WRITE)
    @PostMapping("/importDocumentTable")
    public Resp<List<ImportTableResp>> importDocumentTable(@RequestBody ImportDocumentTableResp req) {
        if (!FileUtil.exist(req.getFilePath())) {
            throw new BizException("请选择文件");
        }
        WordTableManager wordTableManager = WordTableManager.getInstance(req.getFilePath());
        List<ImportTableResp> respList = genTableService.importWordTable(req, wordTableManager);
        return Resp.ok(respList);
    }

    @ApiOperation(value = "导入数据库的表")
    @RamResource(action = "lcp:genTable:importDbTable", name = "低代码-导入数据库的表", type = ActionType.WRITE)
    @PostMapping("/importDbTable")
    public Resp<List<ImportTableResp>> importDbTable(@RequestBody @Validated ImportDbTableReq req) {
        List<ImportTableResp> respList = genTableService.importDbTable(req);
        return Resp.ok(respList);
    }

    @ApiOperation(value = "同步数据库表信息")
    @RamResource(action = "lcp:genTable:syncDbTable", name = "低代码-同步数据库表信息", type = ActionType.WRITE)
    @PostMapping("/syncDbTable")
    public Resp<?> syncDbTable(@RequestParam("id") Long id) {
        genTableService.syncDbTable(id);
        return Resp.ok();
    }

    @ApiOperation(value = "查询表的索引")
    @RamResource(action = "lcp:genTable:listGenTableIndex", name = "低代码-查询表的索引", type = ActionType.READ)
    @GetMapping("/listGenTableIndex")
    public Resp<List<GenTableIndexResp>> listGenTableIndex(@RequestParam("tableId") Long tableId) {
        List<GenTableIndexResp> resp = genTableIndexService.listGenTableIndexByTableId(tableId);
        return Resp.ok(resp);
    }

    @ApiOperation(value = "删除表的索引")
    @RamResource(action = "lcp:genTable:deleteIndex", name = "低代码-删除表的索引", type = ActionType.WRITE)
    @DeleteMapping("/batchDeleteGenTableIndex")
    public Resp<?> batchDeleteGenTableIndex(@RequestParam("ids") List<Long> ids) {
        genTableIndexService.deleteGenTableIndexByIds(ids);
        return Resp.ok();
    }

    @ApiOperation(value = "删除表的列")
    @RamResource(action = "lcp:genTable:deleteColumn", name = "低代码-删除表的列", type = ActionType.WRITE)
    @DeleteMapping("/batchDeleteGenTableColumn")
    public Resp<?> batchDeleteGenTableColumn(@RequestParam("ids") List<Long> ids) {
        genTableColumnService.deleteGenTableColumnByIds(ids);
        return Resp.ok();
    }

    @ApiOperation(value = "批量删除表")
    @RamResource(action = "lcp:genTable:deleteTable", name = "低代码-删除表", type = ActionType.WRITE)
    @DeleteMapping("/batchDeleteGenTable")
    public Resp<?> deleteGenTableByIds(@RequestParam(value = "ids") List<Long> tableIds) {
        genTableService.deleteGenTableByIds(tableIds);
        return Resp.ok();
    }

    @ApiOperation(value = "删除表")
    @RamResource(action = "lcp:genTable:deleteTable", name = "低代码-删除表", type = ActionType.WRITE)
    @DeleteMapping("/deleteGenTable")
    public Resp<?> deleteGenTableById(@RequestParam(value = "id") Long id) {
        genTableService.deleteGenTableByIds(Collections.singletonList(id));
        return Resp.ok();
    }

    @ApiOperation(value = "查询生成代码表的列")
    @RamResource(action = "lcp:genTable:queryTableColum", name = "低代码-查询生成代码表的列", type = ActionType.READ)
    @GetMapping("/listGenTableColumn")
    public Resp<List<GenTableColumnResp>> listGenTableColumn(@RequestParam("tableId") Long tableId) {
        List<GenTableColumnResp> resp = genTableColumnService.listGenTableColumnByTableId(tableId);
        return Resp.ok(resp);
    }

    @ApiOperation(value = "保存或者更新生成代码表的信息")
    @RamResource(action = "lcp:genTable:save", name = "低代码-保存生成代码表的信息", type = ActionType.WRITE)
    @PostMapping("/saveOrUpdateGenTableInfo")
    public Resp<?> saveOrUpdateGenTableInfo(@RequestBody @Validated GenTableInfoSaveReq req) {
        genTableService.saveOrUpdateGenTableInfo(req);
        return Resp.ok();
    }

    @ApiOperation(value = "获取生成代码信息")
    @RamResource(action = "lcp:genTable:getGenInfo", name = "低代码-获取生成代码信息", type = ActionType.READ)
    @GetMapping("/getGenInfo")
    public Resp<GenInfoResp> getGenInfo(@RequestParam(value = "tableId", required = false)
                                            @NotNull(message = "请选择一张表") Long tableId) {
        GenInfoResp resp = genTableService.getGenInfo(tableId);
        return Resp.ok(resp);
    }
}
